<?php
/**
 * Created by PhpStorm.
 * User: Dell
 * Date: 2019-3-8
 * Time: 9:17
 */

namespace app\index\controller;


use app\common\model\MemberModel;
use think\Controller;
use think\Session;

class Login extends Controller
{

    //登陆
    public function index(){
        if (request()->isPost()){


            $req = request()->post();

            if (!Session::get('token') || !isset($req['token']) || Session::get('token') != $req['token']){
                return json(msg('-1','','滑块超时'));

            }

            //账号验证
            if(empty($req['phone'])){
                return json(msg('-1','','手机号不能为空'));
            }else{
                //匹配所有的手机号，不区分哪个运营商，不考虑卫星通信、物联网等特殊号段
                if (!preg_match("/^1[3456789]\d{9}$/",$req['phone'])){
                    return json(msg('-1','','请输入正确的手机号'));

                }
            }

            //手机号密码验证
            $member = new MemberModel();
            $userinfo = $member->memberPhone($req['phone']);
            if ($userinfo){
                if (md5(md5($req['password'])) == $userinfo['memb_password']){

                        $member->editMemberOpenid(['memb_id'=>$userinfo['memb_id'], 'memb_openid'=>Session::get('openid')]);



                    //保存userid
                    Session::set('id',$userinfo['memb_id']);
                    Session::set('name',$userinfo['memb_name']);
                    Session::set('phone',$userinfo['memb_phone']);

                    return json(msg('1','','登陆成功'));
                }
            }
            return json(msg('-1','','账号或者密码错误'));

        }
    }

    //安全退出
    public function logout(){
        Session::delete('id');
        Session::delete('name');
        Session::delete('phone');
        Session::clear();//情况session
        //重定向
        $this->redirect('index/login');
    }

    //修改密码
    public function memberPwd(){
        if (request()->isAjax()){
            $param = input("param.");

//            if (empty($param['password_new']) || empty($param['pwd_new']) || empty($param['password'])){
////                var_dump($param);
//                return json(msg(-1, '', '密码不能为空'));
//            }
//
//            if ($param['password_new'] != $param['pwd_new']){
//                return json(msg(-1, '', '新密码必须一样'));
//            }
//
//            //以字母开头，长度在6~18之间，只能包含字母、数字和下划线
//            if (!preg_match("/^[a-zA-Z\d_]{6,}$/",$param['pwd_new'])){
//                die(json_encode(['code'=>400,'msg'=>'密码长度不能小于6位']));
//            }


//            $user = $member->getOneMember($id);
//
//            if (md5(md5($param['password'])) != $user['memb_password']){
//                return json(msg(-1, '', '原密码错误'));
//
//            }
//
//            if ($user['memb_password'] == md5(md5($param['password_new']))){
//                return json(msg(-1, '', '原密码不能和新密码一样'));
//
//            }
            if (Session::get('Pcode') != $param['code']){
                return json(msg(-1, '', "手机验证码错误"));
            }
            $member = new MemberModel();
            $id = Session::get('id');

            $param['memb_id'] = $id;

            $param['memb_password'] = md5(md5($param['password']));
            unset($param['code']);
            unset($param['password']);
            $flag = $member->memberPwd($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));

        }
    }

    //会员注册
    public function memberAdd(){
        if (request()->isAjax()){
            $param = input("param.");

            if (empty($param['memb_password']) || empty($param['memb_pwd'])){
//                var_dump($param);
                return json(msg(-1, '', '密码不能为空'));
            }

            if ($param['memb_password'] != $param['memb_pwd']){
                return json(msg(-1, '', '新密码必须一样'));
            }

            //以字母开头，长度在6~18之间，只能包含字母、数字和下划线
            if (!preg_match("/^[a-zA-Z\d_]{6,}$/",$param['memb_password'])){
                die(json_encode(['code'=>400,'msg'=>'密码长度不能小于6位']));
            }
            if (Session::get('openid')){
                $param['memb_openid'] = Session::get('openid');
            }
            $param['memb_black'] = 0;//黑名单：否
            $param['memb_password'] = md5(md5($param['memb_password']));//双MD5密码加密
            $param['memb_register'] = date('Y-m-d H:i:s',time());//注册时间
            unset($param['memb_pwd']);

            $member = new MemberModel();
            $flag = $member->addMember($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));

        }
    }


}